package com.mactivity.database;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.mactivity.helper.Constants;

public class DeleteFriendActivityFunctions {
	/**
	 * get the list of friend id 
	 * @param userID of the group plan inviter
	 * @return
	 */
	public String[] getFriendIdList(String userID){
		Log.d(Constants.LOG_DATABASE_TAG, "get friend ID List method called");
		String friendList=null;
		String[] friendIdList=null;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("userId", userID));
		try {
			// fetch all friends' id 
			friendList = CustomHttpClient.executeHttpPost(
					"http://omega.uta.edu/~hbs1966/db_files/PlanActivity/fetch_friend_id_list.php",
					postParameters);
			Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved: "+friendList);
			if(!friendList.equals(null)){	// have friend
				JSONArray jArray = new JSONArray(friendList);
				friendIdList = new String[jArray.length()];
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject json_data = jArray.getJSONObject(i);
					// user id on friend list table is same as inviter id
					if(String.valueOf(json_data.getInt(Constants.COLUMN_FRIEND_TABLE_USER_ID)).compareTo(userID)==0)
						// store friend id from friend table
						friendIdList[i]=String.valueOf(json_data.getInt(Constants.COLUMN_FRIEND_TABLE_FRIEND_ID));
					else	//otherwise, store user id from friend table
						friendIdList[i]=String.valueOf(json_data.getInt(Constants.COLUMN_FRIEND_TABLE_USER_ID));
				}			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOGGER_ERROR_TAG, "Unable to connect to database: "+e.toString());
		}
		return friendIdList;
	}
	
	/**
	 * get the list of the friend name
	 * @param friendIdList list of the friend id
	 * @return a list of the friend name
	 */
	public String[] getFriendNameList(String[] friendIdList){
		Log.d(Constants.LOG_DATABASE_TAG, "get friend name List method called");
		String response=null;
		String [] friendNameList=null;
		friendNameList= new String[friendIdList.length];
		System.out.println(friendIdList.length+" oh now i know");
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		// go through id list for each friend name
		for(int i=0; i<friendIdList.length;i++){
			postParameters.add(new BasicNameValuePair("userId", friendIdList[i]));
			try {
				// fetch friends name
				response = CustomHttpClient.executeHttpPost(
						"http://omega.uta.edu/~hbs1966/db_files/PlanActivity/fetch_friend_name.php",
						postParameters);
				Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved: "+response);
					
				JSONArray jArray = new JSONArray(response);
				// get the friend name
				JSONObject json_data = jArray.getJSONObject(0);
				// store the friend name in a list
				friendNameList[i]=String.valueOf(json_data.getString((Constants.COLUMN_USER_NAME)));
			}
			catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d(Constants.LOGGER_ERROR_TAG, "Unable to connect to database: "+e.toString());
			}
		}
		return friendNameList;
	}
	/**
	 * get the friend email list
	 * @param friendIdList list of friend id
	 * @return a list of friend email
	 */
	public String[] getFriendEmailList(String[] friendIdList){
		Log.d(Constants.LOG_DATABASE_TAG, "get friend email List method called");
		String response=null;
		String [] friendEmailList=null;
		friendEmailList= new String[friendIdList.length];
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		// go through id list for each friend email
		for(int i=0; i<friendIdList.length;i++){
			postParameters.add(new BasicNameValuePair("userId", friendIdList[i]));
			try {
				// get friend's email
				response = CustomHttpClient.executeHttpPost(
						"http://omega.uta.edu/~hbs1966/db_files/PlanActivity/fetch_friend_email.php",
						postParameters);
				Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved: "+response);
					
				JSONArray jArray = new JSONArray(response);
				// get the friend email
				JSONObject json_data = jArray.getJSONObject(0);
				// store the friend email in a list
				friendEmailList[i]=String.valueOf(json_data.getString((Constants.COLUMN_USER_EMAIL)));
			}
			catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d(Constants.LOGGER_ERROR_TAG, "Unable to connect to database: "+e.toString());
			}
		}
		return friendEmailList;
	}
	public String fetchId(String emailId)
	{
		ArrayList<NameValuePair>postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDEMAILID,emailId));
		String response=null,id = null;
		try 
		{
			response = CustomHttpClient.executeHttpPost("http://omega.uta.edu/~hbs1966/db_files/AddFriend/check_user_script.php",postParameters);
			Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved: "+response);
			JSONArray jArray = new JSONArray(response);
			System.out.println("JSON LENGTH:"+jArray.length());
			for(int i=0;i<jArray.length();i++)
			{
				JSONObject json_data = jArray.getJSONObject(i);
				id=json_data.getInt(Constants.COLUMN_USER_ID)+"";
			}
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOGGER_ERROR_TAG, "Unable to connect to database: "+e.toString());
		}
		return id;
	}
	public String deleteFriend(String myId, String frId) {
		// TODO Auto-generated method stub
		String response1=null;
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_MYID,myId));
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDID,frId));
		try {
			//delete tuple from friend table
			response1=CustomHttpClient.executeHttpPost("http://omega.uta.edu/~hbs1966/db_files/AddFriend/delete_friend_script.php",postParameters);
			Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved: "+" "+response1);
			System.out.println("Response`1: "+response1);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOGGER_ERROR_TAG, "Unable to connect to database: "+e.toString());
		}
		return response1;
	}
}
